package com.drug.service.impl;

import com.drug.domain.*;
import com.drug.mapper.*;
import com.drug.service.PurchaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    private DrugCategoryMapper drugCategoryMapper;
    @Autowired
    private DosageFormMapper dosageFormMapper;
    @Autowired
    private DrugInformationMapper drugInformationMapper;
    @Autowired
    private DrugQualityLevelMapper drugQualityLevelMapper;
    @Autowired
    private ManufacturerMapper manufacturerMapper;
    @Autowired
    private PodiPdMapper podiPdMapper;
    @Autowired
    private DirgRgMapper dirgRgMapper;
    @Autowired
    private SdiDiInfMapper sdiDiInfMapper;

    /**
     * 查询药品分类
     * @return
     */
    @Override
    public Result findAllDC() {
        Result result = new Result();
        List<DrugCategory> drugCategory = drugCategoryMapper.selectAll();
        result.setData(drugCategory);
        return result;
    }

    /**
     * 多条件查询
     * @param map
     * @param page
     * @return
     */
    @Override
    public Result findConditional(Map map, Integer page) {
        //使用分页插件
        PageHelper.startPage(page,5);
        Result result = new Result();
        //药品品目
        DrugAtegory drugAtegory = new DrugAtegory();
        //药品类别
        DrugCategory drugCategory = new DrugCategory();

        String number = (String) map.get("number");
        if (number==null||number.equals("")){
            number=null;
        }else {
            int daNumber = Integer.parseInt(number);
            drugAtegory.setDaNumber(daNumber);
        }
        String diName = (String) map.get("cname");
        if (diName==null||diName.equals("")){
            diName=null;
        }
        String diForm = (String) map.get("jixing");
        if (diForm==null||diForm.equals("")){
            diForm=null;
        }
        String diSpecifications = (String) map.get("guige");
        if (diSpecifications==null||diSpecifications.equals("")){
            diSpecifications=null;
        }
        String diUnit = (String) map.get("danwei");
        if (diUnit==null||diUnit.equals("")){
            diUnit=null;
        }
        String diCc = (String) map.get("cc");
        if (diCc==null||diCc.equals("")){
            diCc=null;
        }
        String diDaCategory = (String) map.get("leibie");
        if (diDaCategory==null||diDaCategory.equals("")){
            diDaCategory=null;
        }else {
            drugCategory.setDcName(diDaCategory);

        }
        String state = (String) map.get("zhuangtai");
        if (state==null||state.equals("")){
            state=null;
        }
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setDiForm(diForm);
        drugInformation.setDiSpecifications(diSpecifications);
        drugInformation.setDiUnit(diUnit);
        drugInformation.setDiCc(diCc);
        drugInformation.setDiState(state);
        drugInformation.setDiName(diName);
        drugInformation.setDiIsdel(0);
        drugInformation.setDrugAtegory(drugAtegory);
        drugInformation.setDrugCategory(drugCategory);
        //多条件查询
        List<DrugInformation> drugInformations = drugInformationMapper.selectConditional1(drugInformation);
        PageInfo<DrugInformation> pageInfo= new PageInfo<DrugInformation>(drugInformations);
        result.setData(pageInfo);
        return result;
    }

    /**
     * 查询所有的剂型
     * @return
     */
    @Override
    public Result findAllDF() {
        Result result = new Result();
        List<DosageForm> dosageForms = dosageFormMapper.selectAll();
        result.setData(dosageForms);
        return result;
    }

    /**
     * 查询所有的质量层次
     * @return
     */
    @Override
    public Result findAllDQL() {
        Result result = new Result();
        List<DrugQualityLevel> drugQualityLevels = drugQualityLevelMapper.selectAll();
        result.setData(drugQualityLevels);
        return result;
    }



    /**
     * 多条件查询药品信息
     * @param map
     * @param page
     * @return
     */
    @Override
    public Result findDI(Map map, Integer page) {
        //使用分页插件
        PageHelper.startPage(page,5);
        Result result = new Result();
        //药品品目
        DrugAtegory drugAtegory = new DrugAtegory();
        //药品类别
        DrugCategory drugCategory = new DrugCategory();
        //药品质量层次
        DrugQualityLevel drugQualityLevel = new DrugQualityLevel();

        String number = (String) map.get("dinumber");
        if (number==null||number.equals("")){
            number=null;
        }
        String diName = (String) map.get("cname");
        if (diName==null||diName.equals("")){
            diName=null;
        }
        String diForm = (String) map.get("jixing");
        if (diForm==null||diForm.equals("")){
            diForm=null;
        }
        String diSpecifications = (String) map.get("guige");
        if (diSpecifications==null||diSpecifications.equals("")){
            diSpecifications=null;
        }
        String diUnit = (String) map.get("danwei");
        if (diUnit==null||diUnit.equals("")){
            diUnit=null;
        }
        String diCc = (String) map.get("cc");
        if (diCc==null||diCc.equals("")){
            diCc=null;
        }
        //药品类别企业
        String qiye = (String) map.get("qiye");
        if (qiye==null||qiye.equals("")){
            qiye=null;
        }else {
            drugAtegory.setDaMName(qiye);
        }
        //药品商品
        String shangpin = (String) map.get("shangpin");
        if (shangpin==null||shangpin.equals("")){
            shangpin=null;
        }else {
            drugAtegory.setDaName(shangpin);
        }

        String lefPprice = (String) map.get("lefPprice");
        if (lefPprice==null||lefPprice.equals("")){
            lefPprice=null;
        }
        String rightPrice = (String) map.get("rightPrice");
        if (rightPrice==null||rightPrice.equals("")){
            rightPrice=null;
        }
        //质量层次
        String cengci = (String) map.get("cengci");
        if (cengci==null||cengci.equals("")||cengci.equals("0")){
            cengci=null;
        }else {
            drugQualityLevel.setDqlId(Integer.parseInt(cengci));
        }
        String leibie = (String) map.get("leibie");
        if (leibie==null||leibie.equals("")||leibie.equals("0")){
            leibie=null;
        }else {
            drugCategory.setDcName(leibie);
        }
        String state = (String) map.get("state");
        if (state==null||state.equals("")){
            state=null;
        }
        //药品信息
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setDiNumer(number);
        drugInformation.setDiName(diName);
        drugInformation.setDiForm(diForm);
        drugInformation.setDiSpecifications(diSpecifications);
        drugInformation.setDiUnit(diUnit);
        drugInformation.setDiCc(diCc);
        drugInformation.setDiTradingStatus(state);
        drugInformation.setDiIsdel(0);
        drugInformation.setDrugAtegory(drugAtegory);
        drugInformation.setDrugCategory(drugCategory);
        drugInformation.setDrugQualityLevel(drugQualityLevel);

        //多条件查询
        List<DrugInformation> drugInformations = drugInformationMapper.selectAllDrug(drugInformation,lefPprice,rightPrice);
        PageInfo<DrugInformation> pageInfo= new PageInfo<DrugInformation>(drugInformations);
        result.setData(pageInfo);
        return result;
    }

    /**
     * ；采购维护数据展示
     * @param map
     * @param page
     * @return
     */
    @Override
    public Result findDI1(Map map, Integer page) {
        //使用分页插件
        PageHelper.startPage(page,5);
        Result result = new Result();
        //药品品目
        DrugAtegory drugAtegory = new DrugAtegory();
        //药品类别
        DrugCategory drugCategory = new DrugCategory();
        //药品质量层次
        DrugQualityLevel drugQualityLevel = new DrugQualityLevel();

        String number = (String) map.get("dinumber");
        if (number==null||number.equals("")){
            number=null;
        }
        String diName = (String) map.get("cname");
        if (diName==null||diName.equals("")){
            diName=null;
        }
        String diForm = (String) map.get("jixing");
        if (diForm==null||diForm.equals("")){
            diForm=null;
        }
        String diSpecifications = (String) map.get("guige");
        if (diSpecifications==null||diSpecifications.equals("")){
            diSpecifications=null;
        }
        String diUnit = (String) map.get("danwei");
        if (diUnit==null||diUnit.equals("")){
            diUnit=null;
        }
        String diCc = (String) map.get("cc");
        if (diCc==null||diCc.equals("")){
            diCc=null;
        }
        //药品类别企业
        String qiye = (String) map.get("qiye");
        if (qiye==null||qiye.equals("")){
            qiye=null;
        }else {
            drugAtegory.setDaMName(qiye);
        }
        //药品商品
        String shangpin = (String) map.get("shangpin");
        if (shangpin==null||shangpin.equals("")){
            shangpin=null;
        }else {
            drugAtegory.setDaName(shangpin);
        }

        String lefPprice = (String) map.get("lefPprice");
        if (lefPprice==null||lefPprice.equals("")){
            lefPprice=null;
        }
        String rightPrice = (String) map.get("rightPrice");
        if (rightPrice==null||rightPrice.equals("")){
            rightPrice=null;
        }
        //质量层次
        String cengci = (String) map.get("cengci");
        if (cengci==null||cengci.equals("")||cengci.equals("0")){
            cengci=null;
        }else {
            drugQualityLevel.setDqlId(Integer.parseInt(cengci));
        }
        String leibie = (String) map.get("leibie");
        if (leibie==null||leibie.equals("")||leibie.equals("0")){
            leibie=null;
        }else {
            drugCategory.setDcName(leibie);
        }
        String state = (String) map.get("state");
        if (state==null||state.equals("")){
            state=null;
        }
        //药品信息
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setDiNumer(number);
        drugInformation.setDiName(diName);
        drugInformation.setDiForm(diForm);
        drugInformation.setDiSpecifications(diSpecifications);
        drugInformation.setDiUnit(diUnit);
        drugInformation.setDiCc(diCc);
        drugInformation.setDiTradingStatus(state);
        drugInformation.setDiIsdel(0);
        drugInformation.setDrugAtegory(drugAtegory);
        drugInformation.setDrugCategory(drugCategory);
        drugInformation.setDrugQualityLevel(drugQualityLevel);

        //多条件查询
        List<DrugInformation> drugInformations = drugInformationMapper.selectAllDrug1(drugInformation,lefPprice,rightPrice);
        PageInfo<DrugInformation> pageInfo= new PageInfo<DrugInformation>(drugInformations);
        result.setData(pageInfo);
        return result;
    }

    /**
     * 查询所有的信息
     * @return
     */
    @Override
    public List<DrugInformation> findAll() {
        return drugInformationMapper.selectAll();
    }

    /**
     * 查询所有的信息
     * @return
     */
    @Override
    public List<DrugInformation> findAll1() {
        return drugInformationMapper.selectAll();
    }


    /**
     * 根据类别id查询名字
     * @param id
     * @return
     */
    @Override
    public String findNameById(Integer id) {
        return drugCategoryMapper.selectNameById(id);
    }

    /**
     * 根据企业id查询名字
     * @param id
     * @return
     */
    @Override
    public String findMfNameById(Integer id) {
        return manufacturerMapper.selectNameById(id);
    }
    /**
     * 根据企业id查询名字
     * @param id
     * @return
     */
    @Override
    public String findDqlNameById(Integer id) {
        return drugQualityLevelMapper.selectNameById(id);
    }

    /**
     * 采购目录选择下的选择添加
     * @param id
     * @return
     */
    @Override
    public Result findPhAdd(Integer[] id) {
        Result<DrugInformation> result = new Result<>();
        int i = 0;
        for (i=0;i<id.length;i++){
            Integer integer = sdiDiInfMapper.selectDiId(id[i]);
            drugInformationMapper.addPh(id[i]);
        }
        result.setMsg("成功");
        return result;
    }
    /**
     * 采购目录选择下的选择添加
     * @param id
     * @return
     */
    @Override
    public Result findPhDelect(Integer[] id,Integer v) {
        Result result = new Result<>();
        int i = 0;
        for (i=0;i<id.length;i++){
            podiPdMapper.delectPh(id[i],v);
        }
        result.setMsg("成功");
        return result;
    }

    @Override
    public Result findPhDelect1(Integer[] id, Integer v) {
        Result result = new Result<>();
        int i = 0;
        for (i=0;i<id.length;i++){
            dirgRgMapper.delectPh(id[i],v);
        }
        result.setMsg("成功");
        return result;
    }
}
